package com.cashmama.market.api.application.controller.authentication;

import com.alibaba.fastjson.JSON;
import com.cashmama.market.api.application.controller.authentication.response.UserProfileResponse;
import com.cashmama.market.api.application.controller.authentication.vos.UserProfileRequest;
import com.cashmama.market.api.application.controller.authentication.vos.UserProfileSaveRequest;
import com.cashmama.market.api.domain.vo.RequestLocalInfo;
import com.cashmama.market.api.infras.consts.RedisKeyConsts;
import com.cashmama.market.api.infras.enums.BasicErrorCodeEnum;
import com.cashmama.market.api.infras.exception.ApplicationException;
import com.cashmama.market.api.infras.handler.ApiResponse;
import com.cashmama.market.api.infras.handler.ResponseData;
import com.fqgj.base.services.redis.RedisClient;
import com.fqgj.common.api.annotations.ParamsValidate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: yujianfu (mr.vencnet@gmail.com)
 * Date: 2019-12-20
 * Time: 16:19
 */
@RestController
@RequestMapping("/user/profile")
public class AuthenticationController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AuthenticationController.class);

    @Autowired
    private RedisClient redisClient;

    public static Map<String, AuthenticationBaseHandler> authenticationBaseHandlerMap = new HashMap<>();

    /**
     * 个人信息获取
     *
     * @param userProfileRequest
     * @return
     */
    @ParamsValidate
    @RequestMapping("/get")
    public ApiResponse<ResponseData> getAuthentication(@RequestBody UserProfileRequest userProfileRequest) {
        return new ApiResponse(authenticationBaseHandlerMap.get(userProfileRequest.getType()).content());
    }

    /**
     * 个人信息保存
     *
     * @param userProfileSaveRequest
     * @return
     */
    @ParamsValidate
    @RequestMapping("/save")
    public ApiResponse<UserProfileResponse> save(@RequestBody UserProfileSaveRequest userProfileSaveRequest) {
        LOGGER.info("保存个人信息,request:{}", JSON.toJSONString(userProfileSaveRequest));
        String userCode = RequestLocalInfo.getCurrentUser().getUserCode();
        if (redisClient.get(RedisKeyConsts.OCR_PAN_LOCK, userCode) != null) {
            LOGGER.info("panCardSave防并发,userCode:{}", userCode);
            throw new ApplicationException(BasicErrorCodeEnum.CLICK_ONLY_ONCE);
        } else {
            redisClient.set(RedisKeyConsts.OCR_PAN_LOCK, userCode, 5, userCode);
        }

        UserProfileResponse userProfileResponse = (UserProfileResponse) authenticationBaseHandlerMap.get(userProfileSaveRequest.getType()).save(userProfileSaveRequest.getParamsList());
        return new ApiResponse(userProfileResponse);
    }
}
